package jeesample.jaxrs;

import java.util.List;

import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import jeesample.dtos.BookingDTO;
import jeesample.dtos.RoomDTO;
import jeesample.service.publishers.BookingServicePublisher;

import org.apache.log4j.Logger;
import org.glassfish.jersey.server.JSONP;

@Path("booking")
public class BookingRS extends BaseRESTfulService {
	private Logger logger = Logger.getLogger(getClass());

	@Override
	protected Logger logger() {
		// TODO Auto-generated method stub
		return logger;
	}

	private BookingServicePublisher publisher = new BookingServicePublisher();

	@GET
	@Path("/rooms/booked")
	@JSONP
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public List<BookingDTO> listBookedRooms() {
		dbg("listBookedRooms()");
		return publisher.listBookedRooms();
	}

	@GET
	@Path("/rooms/available")
	@JSONP
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public List<RoomDTO> listAvailableRooms() {
		dbg("listAvailableRooms()");
		return publisher.listAvailableRooms();
	}

	/*
	 * @POST
	 * 
	 * @Path("/rooms")
	 * 
	 * @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	 * 
	 * @JSONP
	 * 
	 * @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	 * public BookingDTO reserve(@FormParam("roomId") String roomId,
	 * 
	 * @FormParam("bookingId") String bookingId,
	 * 
	 * @FormParam("bookedBy") String bookedBy,
	 * 
	 * @FormParam("startTime") String startTime,
	 * 
	 * @FormParam("endTime") String endTime) { dbg("reserve(" + roomId + "," +
	 * bookingId + "," + bookedBy + "," + startTime + "," + endTime + ")");
	 * 
	 * BookingDTO dto=new BookingDTO(); dto.setBookedBy(bookedBy);
	 * dto.setBookingId(bookingId); dto.setRoomId(roomId);
	 * dto.setStartTime(startTime); dto.setEndTime(endTime); return
	 * publisher.reserve(dto); }
	 */

	@POST
	@Path("/rooms")
	@JSONP
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public BookingDTO reserve(BookingDTO dto) {
		dbg("reserve(" + dto.toString() + ")");
		return publisher.reserve(dto);
	}

	@DELETE
	@Path("/rooms/{bookingId}")
	@JSONP
	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public RoomDTO unreserve(@PathParam("bookingId") String bookingId) {
		dbg("unreserve(" + bookingId + ")");
		return publisher.unreserve(bookingId);
	}
}
